package com.atguigu.base;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

public class BaseController {
    /**
     * 封装页面提交的分页参数及搜索条件
     * @param request
     * @return
     */
    public Map<String,Object> getFilters(HttpServletRequest request){
        Map<String,Object> filters =new LinkedHashMap<>();
        // 获取请求中的所有的参数名
        Enumeration<String> parameterNames = request.getParameterNames();
        while(parameterNames!= null && parameterNames.hasMoreElements()){
            // 获取参数名   roleName pageNum pageSize
            String paramName = parameterNames.nextElement();
            String[] values = request.getParameterValues(paramName);
            if(values.length>1){
                filters.put(paramName,values);
            }else{
                filters.put(paramName,values[0]);
            }
        }

        if(!filters.containsKey("pageNum")){
            filters.put("pageNum",1);
        }

        if(!filters.containsKey("pageSize")){
            filters.put("pageSize",3);
        }
        return filters;
    }
}
